package LCR剑指.LCR073;

import java.util.Arrays;
import java.util.Comparator;

class Solution {
    public static int minEatingSpeed(int[] piles, int h) {

        Arrays.sort(piles);
        //二分查找
        int len = piles.length;
        int min=1;
        int max=piles[len-1];
        int mid=0;
        int minSpeed=Integer.MAX_VALUE;
        while (min<max){
            mid = min+(max-min)/2;
            long eatHours=eatingHours(piles,mid);
            if(eatHours<=h){
                max=mid;
                minSpeed=mid;
            }else {
                min=mid+1;
            }
        }
        return minSpeed;
    }

    public static long eatingHours(int[] piles,int speed) {
        long total = 0;
        for (int i = 0; i < piles.length; i++) {
            total += piles[i]/speed+(piles[i]%speed==0?0:1);
        }
        return total;
    }

    public static void main(String[] args) {
        int i = minEatingSpeed(new int[]{3, 6, 7, 11}, 8);
        System.out.println(i);
    }
}